package com.tedu.ds.equipment.service.impl;

import com.tedu.ds.base.enmu.EmpStatus;
import com.tedu.ds.base.enmu.EquipStatus;
import com.tedu.ds.equipment.mapper.EquipmentMapper;
import com.tedu.ds.equipment.pojo.dto.EquipmentParam;
import com.tedu.ds.equipment.pojo.dto.EquipmentQuery;
import com.tedu.ds.equipment.pojo.entity.Equipment;
import com.tedu.ds.equipment.pojo.vo.EquipmentPage;
import com.tedu.ds.equipment.pojo.vo.EquipmentVO;
import com.tedu.ds.equipment.service.EquipmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public EquipmentPage select(EquipmentQuery equipmentQuery) {
        List<EquipmentVO> list=equipmentMapper.select(equipmentQuery);
        int total=equipmentMapper.selectCount(equipmentQuery);
        EquipmentPage equipmentPage=new EquipmentPage();
        equipmentPage.setTotal(total);
        equipmentPage.setEquipmentVOS(list);
        return equipmentPage;
    }

    @Override
    public void save(EquipmentParam equipmentParam) {
        Equipment equipment=new Equipment();
        BeanUtils.copyProperties(equipmentParam,equipment);
        if (equipment.getId()==null){
            equipment.setStatus(EquipStatus.IDLE.getCode());
            equipmentMapper.insert(equipment);
        }else if (equipment.getStatus().equals(EquipStatus.SCRAPPED.getCode())){
            equipment.setScrapTime(new Date());
            equipmentMapper.update(equipment);
        }else {
            equipmentMapper.update(equipment);
        }
    }

    @Override
    public void delete(Long id) {
        equipmentMapper.delete(id);
    }
}
